package com.atguigu.gmall.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.service.CartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * title:
 * author: Gyh
 * date:2023-05-05 16:52
 * Description:
 */
@RestController
@RequestMapping("/api/cart")
public class CarApiController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param request
     * @param skuId 商品SkuId
     * @param skuNum 商品加购数量
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request,
                            @PathVariable("skuId") Long skuId,
                            @PathVariable("skuNum") Integer skuNum){
        //遍历用户ID:可能是登录用户ID也可能是临时用户ID
        String userId = "";
        userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(userId,skuId,skuNum);
        return Result.ok();
    }

    /**
     * 查询购物车列表
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request){
        //获取用户id
        String userId = AuthContextHolder.getUserId(request);
        //获取用户临时id
        String userTempId = AuthContextHolder.getUserTempId(request);
        //调用业务层合并购物车
        List<CartInfo> cartInfoList = cartService.cartList(userId,userTempId);
        return Result.ok(cartInfoList);
    }

    /**
     * 更新选中状态
     * @param request
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result updateCheck(HttpServletRequest request,
                            @PathVariable("skuId") Long skuId,
                            @PathVariable("isChecked") int isChecked){
        //获取临时用户id和登录用户id
        String userId = getUserId(request);
        cartService.updateCheck(userId,skuId,isChecked);
        return Result.ok();
    }

    /**
     * 删除购物车中商品
     * @param request
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request,
                             @PathVariable("skuId") Long skuId){
        String userId = getUserId(request);
        cartService.deleteCart(userId,skuId);
        return Result.ok();
    }

    /**
     * 获取选中状态的购物车列表
     * @param userId
     * @return
     */
    @GetMapping("/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId){
        return cartService.getCartCheckedList(userId);
    }

    /**
     * 尝试获取临时用户id和登录用户id
     * @param request
     * @return
     */
    private String getUserId(HttpServletRequest request){
        String userId = "";
        String userTempId = AuthContextHolder.getUserTempId(request);
        if (StringUtils.isNotBlank(userTempId)) {
            userId = userTempId;
        }
        String loginUserId = AuthContextHolder.getUserId(request);
        if (StringUtils.isNotBlank(loginUserId)) {
            userId = loginUserId;
        }
        return userId;
    }
}
